/*   
 * Copyright (c) 2011, TUgether, TU Braunschweig
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 /**
 * @author Mohamed Karray <mohamed.karray@gmx.de>
 *
 */
 

package de.tu_bs.tugether.liferayunit.liferayElements;

import java.io.IOException;

import net.sourceforge.htmlunit.corejs.javascript.*;

import com.gargoylesoftware.htmlunit.ConfirmHandler;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlListItem;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

import de.tu_bs.tugether.liferayunit.exceptions.LiferayElementNotFoundException;
import de.tu_bs.tugether.liferayunit.exceptions.LiferayPropertyKeyNotFoundException;
import de.tu_bs.tugether.liferayunit.exceptions.LiferayUnitException;
import de.tu_bs.tugether.liferayunit.liferayPages.LiferayPage;

public class Tab extends LiferayElement {

	protected static final String XPATH_KEY_TAB_ANCHOR = "tab-anchor";
	protected static final String XPATH_KEY_DELETE_BUTTON = "delete-button";
	protected static final String XPATH_KEY_IS_SELECTED = "is-selected";
	protected static final String XPATH_KEY_INPUT = "input";
	protected static final String XPATH_KEY_INPUT_SAVE = "input.save-button";
	protected static final String XPATH_KEY_MANAGE_PAGE = "manage-page-anchor";

	private HtmlAnchor tabLink;
	private HtmlSpan delete;

	public Tab(HtmlListItem htmlElement, LiferayPage lfrPage) throws IOException, LiferayElementNotFoundException {
		super(htmlElement, lfrPage);
	}

	@Override
	protected void initialize() throws LiferayElementNotFoundException {
		try {
			this.tabLink = this.<HtmlAnchor> getFirstByXPathKey(XPATH_KEY_TAB_ANCHOR);
		} catch (LiferayUnitException e) {
			this.tabLink = null;
		}
		try {
			this.getHtmlElement().mouseMove();
			this.delete = this.<HtmlSpan> getFirstByXPathKey(XPATH_KEY_DELETE_BUTTON);
		} catch (LiferayUnitException e) {
			this.delete = null;
		}
	}

	public String getTabName() throws LiferayElementNotFoundException {
		if (this.tabLink == null)
			throw new LiferayElementNotFoundException();
		else
			return this.tabLink.asText();
	}

	public boolean isSelected() {
		try {
			HtmlElement el = this.getFirstByXPathKey(XPATH_KEY_IS_SELECTED);
			return el != null;
		} catch (LiferayUnitException e) {
			return false;
		}
	}

	public HtmlPage click() throws IOException {
		HtmlPage click = null;
		boolean throwed = false;
		for(int i = 0; i<10 && !throwed; i++){
			try{
				click = tabLink.click();
				throwed = true;
			} catch(WrappedException e){
				throwed = false;
			}
		}
		return click;
	}

	public void rename(String newTabName) throws IOException, LiferayPropertyKeyNotFoundException,
			LiferayElementNotFoundException {
		HtmlSpan span = (HtmlSpan) this.getHtmlElement().getElementsByTagName("span").get(0);
		span.click();
		this.getLiferayPage().getWebClient().waitForBackgroundJavaScript(3000);
		HtmlTextInput input = this.<HtmlTextInput> getFirstByXPathKey(XPATH_KEY_INPUT);
		HtmlButton save = this.<HtmlButton> getFirstByXPathKey(XPATH_KEY_INPUT_SAVE);
		input.setValueAttribute(newTabName);
		save.click();
	}

	public void delete() throws IOException, LiferayElementNotFoundException, LiferayPropertyKeyNotFoundException {
		this.getLiferayPage().getWebClient().setConfirmHandler(new ConfirmHandler() {

			@Override
			public boolean handleConfirm(Page arg0, String arg1) {
				return true;
			}
		});
		this.delete = this.<HtmlSpan> getFirstByXPathKey(XPATH_KEY_DELETE_BUTTON);
		this.delete.mouseMove();
		this.delete.click();
		this.getLiferayPage().initializeTabList();

	}

	public void addSubTab(String title) throws Exception {
		getLiferayPage().goToTab(this.getTabName());
		getLiferayPage().addSubTab(title);
	}

	public boolean isAlive() {
		return this.tabLink != null;
	}

}
